package pe.com.modulo.cliente;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.sunarp.vehiculo.WSInfoSunarpProxy;

import ws.reniec.persona.WSInfoReniecProxy;


@SuppressWarnings("serial")
@WebServlet("/buscarpersona")
public class clienteServlet extends HttpServlet{
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String sDNI = request.getParameter("strDNI");

		Gson gson = new Gson();
		String json = null;
		
		String data = obtenerDatosDeCliente(sDNI);
		String datosArray[] = data.split("&");
		clienteBean cliente = new clienteBean();
		cliente.setNombres(datosArray[0].toString());
		cliente.setApellidos(datosArray[1].toString());
		cliente.setDni(datosArray[2].toString());
		cliente.setEmail(datosArray[3].toString());
		cliente.setFec_nacimiento(datosArray[4].toString());
		cliente.setSexo(datosArray[5].toString());
		cliente.setDni(sDNI);
		
		json = gson.toJson(cliente);

		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		//response.setContentType("application/json");
		PrintWriter out = response.getWriter();		
		out.print(json);
		out.flush();
	}	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("mmmmmmmmmmmmmm");
	}

	
	public String obtenerDatosDeCliente(String sDNI){
		
		WSInfoReniecProxy proxy = new WSInfoReniecProxy();
		
		String data = null;
		
		try {
			
			data = proxy.buscarUsuarioPorDNI(sDNI);
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return data;
	}

}
